<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The SplFixedArray class</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="splpriorityqueue.valid.html">SplPriorityQueue::valid</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="splfixedarray.construct.html">SplFixedArray::__construct</a></div>
 <div class="up"><a href="spl.datastructures.html">数据结构</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="class.splfixedarray" class="reference">
 <h1 class="title">The SplFixedArray class</h1>
 
 
 <div class="partintro"><p class="verinfo">(PHP 5 &gt;= 5.3.0)</p>
 

  <div class="section" id="splfixedarray.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    The SplFixedArray class provides the main functionalities of array. The 
    main differences between a SplFixedArray and a normal PHP array is that 
    the SplFixedArray is of fixed length and allows only integers within 
    the range as indexes. The advantage is that it allows a faster array
    implementation.
   </p>
  </div>

 
  <div class="section" id="splfixedarray.synopsis">
   <h2 class="title">类摘要</h2>
 

   <div class="classsynopsis">
    <div class="ooclass"></div>
 


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <strong class="classname">SplFixedArray</strong>
     </span>

     <span class="oointerface">implements 
      <span class="interfacename"><strong class="interfacename">Iterator</strong></span>
     </span>

     <span class="oointerface">, 
      <span class="interfacename"><strong class="interfacename">ArrayAccess</strong></span>
     </span>
 
     <span class="oointerface">, 
      <span class="interfacename"><a href="class.countable.html" class="interfacename">Countable</a></span>
     </span>
 
     {</div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.construct.html" class="methodname">__construct</a></span>
    ([ <span class="methodparam"><span class="type">int</span> <code class="parameter">$size</code><span class="initializer"> = 0</span></span>
  ] )</div>

    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">int</span><span class="methodname"><a href="splfixedarray.count.html" class="methodname">count</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">mixed</span><span class="methodname"><a href="splfixedarray.current.html" class="methodname">current</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">SplFixedArray</span><span class="methodname"><a href="splfixedarray.fromarray.html" class="methodname">fromArray</a></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$save_indexes</code><span class="initializer"> = true</span></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">int</span><span class="methodname"><a href="splfixedarray.getsize.html" class="methodname">getSize</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">int</span><span class="methodname"><a href="splfixedarray.key.html" class="methodname">key</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">void</span><span class="methodname"><a href="splfixedarray.next.html" class="methodname">next</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span><span class="methodname"><a href="splfixedarray.offsetexists.html" class="methodname">offsetExists</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">mixed</span><span class="methodname"><a href="splfixedarray.offsetget.html" class="methodname">offsetGet</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">void</span><span class="methodname"><a href="splfixedarray.offsetset.html" class="methodname">offsetSet</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$newval</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">void</span><span class="methodname"><a href="splfixedarray.offsetunset.html" class="methodname">offsetUnset</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">void</span><span class="methodname"><a href="splfixedarray.rewind.html" class="methodname">rewind</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">int</span><span class="methodname"><a href="splfixedarray.setsize.html" class="methodname">setSize</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$size</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="splfixedarray.toarray.html" class="methodname">toArray</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span><span class="methodname"><a href="splfixedarray.valid.html" class="methodname">valid</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">void</span><span class="methodname"><a href="splfixedarray.wakeup.html" class="methodname">__wakeup</a></span>
    ( <span class="methodparam">void</span>
   )</div>

   }</div>
 
  </div>

  <div class="section" id="splfixedarray.examples">
   <h2 class="title">范例</h2>
   <p class="para">
    <div class="example" id="example-3707">
     <p><strong>Example #1 <strong class="classname">SplFixedArray</strong> usage example</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Initialize&nbsp;the&nbsp;array&nbsp;with&nbsp;a&nbsp;fixed&nbsp;length<br /></span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SplFixedArray</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">4</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);&nbsp;</span><span style="color: #FF8000">//&nbsp;NULL<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);&nbsp;</span><span style="color: #FF8000">//&nbsp;int(2)<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">"4"</span><span style="color: #007700">]);&nbsp;</span><span style="color: #FF8000">//&nbsp;string(3)&nbsp;"foo"<br /><br />//&nbsp;Increase&nbsp;the&nbsp;size&nbsp;of&nbsp;the&nbsp;array&nbsp;to&nbsp;10<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setSize</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">9</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">"asdf"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Shrink&nbsp;the&nbsp;array&nbsp;to&nbsp;a&nbsp;size&nbsp;of&nbsp;2<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setSize</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;following&nbsp;lines&nbsp;throw&nbsp;a&nbsp;RuntimeException:&nbsp;Index&nbsp;invalid&nbsp;or&nbsp;out&nbsp;of&nbsp;range<br /></span><span style="color: #007700">try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">"non-numeric"</span><span style="color: #007700">]);<br />}&nbsp;catch(</span><span style="color: #0000BB">RuntimeException&nbsp;$re</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"RuntimeException:&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">$re</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">().</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br />try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[-</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br />}&nbsp;catch(</span><span style="color: #0000BB">RuntimeException&nbsp;$re</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"RuntimeException:&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">$re</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">().</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br />try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">5</span><span style="color: #007700">]);<br />}&nbsp;catch(</span><span style="color: #0000BB">RuntimeException&nbsp;$re</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"RuntimeException:&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">$re</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">().</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
NULL
int(2)
string(3) &quot;foo&quot;
RuntimeException: Index invalid or out of range
RuntimeException: Index invalid or out of range
RuntimeException: Index invalid or out of range
</pre></div>
     </div>
    </div>
   </p>
  </div>

 

 
 </div>
 
 
































































































 
<h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="splfixedarray.construct.html">SplFixedArray::__construct</a> — Constructs a new fixed array</li><li><a href="splfixedarray.count.html">SplFixedArray::count</a> — Returns the size of the array</li><li><a href="splfixedarray.current.html">SplFixedArray::current</a> — Return current array entry</li><li><a href="splfixedarray.fromarray.html">SplFixedArray::fromArray</a> — Import a PHP array in a SplFixedArray instance</li><li><a href="splfixedarray.getsize.html">SplFixedArray::getSize</a> — Gets the size of the array</li><li><a href="splfixedarray.key.html">SplFixedArray::key</a> — Return current array index</li><li><a href="splfixedarray.next.html">SplFixedArray::next</a> — Move to next entry</li><li><a href="splfixedarray.offsetexists.html">SplFixedArray::offsetExists</a> — Returns whether the requested index exists</li><li><a href="splfixedarray.offsetget.html">SplFixedArray::offsetGet</a> — Returns the value at the specified index</li><li><a href="splfixedarray.offsetset.html">SplFixedArray::offsetSet</a> — Sets a new value at a specified index</li><li><a href="splfixedarray.offsetunset.html">SplFixedArray::offsetUnset</a> — Unsets the value at the specified $index</li><li><a href="splfixedarray.rewind.html">SplFixedArray::rewind</a> — Rewind iterator back to the start</li><li><a href="splfixedarray.setsize.html">SplFixedArray::setSize</a> — Change the size of an array</li><li><a href="splfixedarray.toarray.html">SplFixedArray::toArray</a> — Returns a PHP array from the fixed array</li><li><a href="splfixedarray.valid.html">SplFixedArray::valid</a> — Check whether the array contains more elements</li><li><a href="splfixedarray.wakeup.html">SplFixedArray::__wakeup</a> — Reinitialises the array after being unserialised</li></ul>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="splpriorityqueue.valid.html">SplPriorityQueue::valid</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="splfixedarray.construct.html">SplFixedArray::__construct</a></div>
 <div class="up"><a href="spl.datastructures.html">数据结构</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
